    ### DATAPACK CREATED BY: ...standing on the shoulders of giants...###

...no really...

##################
# INFERNALDEVICE #
##################
Seriously, this pack wouldn't have been possible without their extensive informative support. Their youtube videos and accompanying datapacks have made projects like this possible. Without them, it would still be stuck in one of the many "Ask 4 Help" forums. Please, check out InfernalDevice's work at their youtube channel: https://www.youtube.com/@InfernalDevice I'm more than willing to wager, you'll learn something not just new, but useful to your datapack ambitions!

#############
# GALSERGEY #
#############
The other giant, whose brain has been picked through the years of reddit posts/comments/responses is GalSergy. You can find their irreplaceable information on reddit: https://www.reddit.com/user/GalSergey/

###############################
# Various Communities & Tools #
###############################
In no particular order: https://mcstacker.net/  https://www.reddit.com/r/MinecraftCommands/  https://misode.github.io/  Cloud Wolf MC's discord community:https://discord.gg/VbsGPB3xmz  https://datapack.wiki/  https://minecraft.wiki/  https://www.plotz.co.uk/ (Amaing that site is still goin' strong!)

A *massive*...

 /$$$$$$$$ /$$   /$$  /$$$$$$  /$$   /$$ /$$   /$$       /$$     /$$ /$$$$$$  /$$   /$$
|__  $$__/| $$  | $$ /$$__  $$| $$$ | $$| $$  /$$/      |  $$   /$$//$$__  $$| $$  | $$
   | $$   | $$  | $$| $$  \ $$| $$$$| $$| $$ /$$/        \  $$ /$$/| $$  \ $$| $$  | $$
   | $$   | $$$$$$$$| $$$$$$$$| $$ $$ $$| $$$$$/          \  $$$$/ | $$  | $$| $$  | $$
   | $$   | $$__  $$| $$__  $$| $$  $$$$| $$  $$           \  $$/  | $$  | $$| $$  | $$
   | $$   | $$  | $$| $$  | $$| $$\  $$$| $$\  $$           | $$   | $$  | $$| $$  | $$
   | $$   | $$  | $$| $$  | $$| $$ \  $$| $$ \  $$          | $$   |  $$$$$$/|  $$$$$$/
   |__/   |__/  |__/|__/  |__/|__/  \__/|__/  \__/          |__/    \______/  \______/ 

...to all of those mentioned! (Font made using https://patorjk.com/software/taag/)

#########################################################################################################################

    ##############################
    ## HOW TO USE THIS DATAPACK ##
    ##############################
## Data Driven Trades:##
- These changes require no special action from users to experience! Librarians just don't seel enchanted books by default any more.

## Teach Master level Librarians max level enchantments: ##
- Bring the maximum level enchantment you want, to a 'MASTER' level 'LIBRARIAN' to sell. 'RIGHT CLICK' on this 'LIBRARIAN'. 
!⯀!NOTE: The Librarian trade GUI must be exited before the learned trade will appear!⯀!

!⯀!NOTE: ONLY ONE ENCHANTMENT CAN BE LEARNED AT A TIME! 
- IF THERE ARE MULTIPLE ENCHANTMENTS ON A SINGLE ENCHANTED BOOK, ONLY THE MAXIMUM LEVEL ENCHANTMENT WITH THE EARLIEST APPEARANCE IN AN ALPHABETICAL ORDERED LIST WILL BE LEARNED. ALL OTHER ENCHANTMENTS WILL BE DELETED. 
- CURSES ARE AFTER THE ENTIRE ALPHABET!⯀!

## Separate multi-enchanted books into individual enchanted books: ##
-Separate multiple enchantments from a single enchanted book by holding the 'MULTI-ENCHANTED BOOK' in the 'OFFHAND', enough 'BOOKS' in the 'MAIN HAND', and then 'RIGHT CLICK' on a 'GRIND STONE'. 
-(If there is only 1 enchantment, or not enough books in the main hand, an error message will indicate such.)

!⯀!NOTE: After the enchantments separate, the grindstone GUI will still be open and the multi-enchanted book will still be in the off hand. Rest assured, this is just a visual artifact. Even 'swapping hands' won't bring that book back into reality!⯀!

###########################################################################################################################################

    ############################
    ## HOW THIS DATAPCK WORKS ##
    ############################
This datapack is made up of 5 main systems:
- Data Driven Trades
- Teaching Enchantments
- Separating Multi-Enchanted Books

###########################################################################################################################################

    ##########################
    ### DATA DRIVEN TRADES ###
    ##########################
This feature, simply removes all enchanted book offerings from Librarians at each level.
    #############################
    # How this is accomplished: #
Three files, in three distinct locations are required. (Please, read slowly as there a duplicate names, but none are the same...thanks Mojang :/)
- data / minecraft / trade_set / <profession> / level_<#>.json
- data / minecraft / tags / villager_trade / <profession> / level_<#>.json
- data / minecraft / villager_trade / <profession> / <#> / <trade file name>.json

#####################################################################
# 'level_<#>.json' from 'data / minecraft / trade_set/<profession>/'#
This is the 1st in the series to be called upon. It specifics the 'amount' of trades that will be offered at the specified level of the profession (1=Novice / 2=Amateur / 3=Journeyman / 4=Expert / 5=Master). As well, what 'trades' will be offered (found in a different file). There's also the 'random_sequence' which...seems silly to this author as I haven't figured out how to make it do anything other than...randomize the given trades.

!⯀!NOTE: "#minecraft:<profession> / level_<#>.json" actually specifies the file located at 'data / minecraft / tags / villager_trade /'!⯀!

####################################################################################
# 'level_<#>.json' from 'data / minecraft / tags / villager_trade / <profession> /'#
This file contains a list of all 'trade files' offered at a given level of the profession, and where they're located. Example: "minecraft:librarian/1/emerald_bookshelf" = the file 'emerald_bookshelf.json' located in 'data / minecraft / villager_trade / 1 /'
!!!NOTE: Addition of the line '"replace": true,' MUST be added to this file when located inside of the 'minecraft' folder. If not, the datapack specified trades, as well as the default trades will appear intermingled.

###########################################################################################
# '<trade file name>.json' from 'data / minecraft / villager_trade / <profession> / <#> /'#
This file contains the actual items, quantities, modifiers, villager experience etc. All the details of a normal villager trade. Use Misode's tool to help understand and generate your own! https://misode.github.io/villager-trade/ 

▲TIP: If you're building your own trades, and an error like this comes up when using '/reload':
'Couldn't load tag minecraft:<profession>/level_<#> as it is missing following references: minecraft:<profession>/<#>/<trade_file_name.json>  (from file/<datapack_name>)'
If the files are where they're supposed to be, close out of the world, and re-enter. If they're where they're supposed to be and the world won't load back in, this means there's an error in the file/folder name.▲

!⯀!NOTE: Custom files/folders can be used, but require a different folder structure, and don't require the `"replace": true,` addition. The provided noted above are for files ONLY located within the 'minecraft' folder!⯀!

###########################################################################################################################################

    #############################
    ### TEACHING ENCHANTMENTS ###
    #############################
This system enables Master level Librarians, to learn & sell maximum level enchantments, such as Sharpness V, Unbreaking III, etc. But not enchantments such as Lure II and Fire Aspect I, as those are not the maximum possible level for them.

    #############################
    # How this is accomplished: #
1: An advancement 'teach_librarian_advancement' detects when a player, holding an enchanted book, right clicks on a Master level Librarian.

2. Reward function 'teach_librarian_reward_tutor_raycast_setup' then does a bunch of work: Remove the triggering advancement > Tags the Player who triggered the advancement as 'TUTOR' > Sets the 'steps' for the ray cast to 33 (3.3 blocks is the interaction distance) > runs the actual raycast operation function 'tutor_raycast_operation' which identifies if the player is looking at villager, and if so runs 'tutor_raycast_success' which tags the targeted villager with 'PUPIL_LIBRARIAN'.

3. After the ray cast has tagged the librarian in question, function 'enchantment_lesson_select' is ran. This checks the book in the hand of the player, to see what maximum enchantment exists on the enchanted book. 
!!!NOTE: ONLY ONE ENCHANTMENT CAN BE LEARNED AT A TIME. IF THERE ARE MULTIPLE ENCHANTMENTS ON A SINGLE ENCHANTED BOOK, ONLY THE MAXIMUM LEVEL ENCHANTMENT WITH THE EARLIEST APPEARANCE IN AN ALPHABETICAL ORDERED LIST WILL BE LEARNED. ALL OTHER ENCHANTMENT WILL BE DELETED.CURSES ARE AFTER THE ENTIRE ALPHABET!!!

4. If the max level enchantment is found, AND the selected villager hasn't already learned this enchantment, then the function '<enchantment>_<max level>_lesson' runs.

5. '<enchantment>_<max level>_lesson' then adds, to the end of the current list of trades offered by the targeted Master Librarian, the specified enchantment. The prices are determined by the level, and if the enchantment is considered a 'treasure'. (Lvl 1 = 5 Emeralds, Lvl 2 = 8, Lvl 3 = 11, Lvl 4 = 14, Lvl 5 = 17, Treasure = 2x Lvl cost). The player held enchanted book is deleted, and a 'Villager celebrates' sound is played to the player.

!⯀!NOTE: The prices can be edited by replacing the value located between the '<>' in the following command: 'data modify entity @s Offers.Recipes append value {maxUses:<#>,priceMultiplier:0.2f,buy:{id:"minecraft:emerald",count:<#>},buyB:{id:"minecraft:book",count:<#>},sell:{id:"minecraft:enchanted_book",count:1,components:{"minecraft:stored_enchantments":{"minecraft:<enchantment>":<level #>}}}}'!⯀!

###########################################################################################################################################

    ########################################
    ### SEPARATING MULTI-ENCHANTED BOOKS ###
    ########################################
This system enables any 'Enchanted Book', with multiple enchantments, to have each individual enchantment extracted to it's own book. This only happens when:
- The player has the multi-enchanted book in their off hand.
- The Player has enough empty books in their main hand. (1 fewer than the total enchantments, as the multi-enchanted book, is counted as...a book.)
- With the previous two condition met, the player 'right clicks' on a grind stone. Then the system operates.

    #############################
    # How this is accomplished: #
1.The advancement 'separate_enchantments_advancement' triggers if the player has an enchanted book in their off hand, a book in their main hand, and if they 'right clicked' on a grind stone.

2. 'separate_enchantments_reward' 1st runs to count the number of enchantments present on the enchanted book in the players offhand by running...

3. 'counting_inspect_enchantments' this function takes an enchantment from the list of all enchantments and then runs #4 before removing the selected enchantment from the list, and moving onto the next.

4. 'counting_verify_enchantment_and_level' this function finally takes the selected enchantment from the previous function, and check every level of it, to see if it's present on the enchanted book. If it is, the score for 'CURRENT_ENCHANTMENTS' in "ENCHANTMENT_COUNT" is increased by '1'. Once this function completes, step #3 removes the recently checked-for enchantments, and selected another, repeating #4 until all enchantments and their levels have been counted.

5. Back to 'separate_enchantments_reward', if the count of enchantments is 1, then an error message is displayed indicating such. After that, the number of books present in the players main hand are counted, and if that number is less than the count of enchantments on the enchanted book in the offhand, an error message informs the player. Assuming these checks are passed, then...

6. 'give_inspect_enchantments' this does the same exact as step #3. It just passes an enchantment from the list of all enchantments over to 'give_verify_enchantment_and_level' before removing the current enchantment form the list, and moving onto the next one.

7. 'give_verify_enchantment_and_level' does the same as 'count_verify_enchantment_and_level' but instead of counting enchantments, upon success, this function runs...

8. 'give_enchanted_book' which give the player an enchanted book with the enchantment and level that was passed from the previous functions. This also decreases the score of 'CURRENT_ENCHANTMENTS' for 'ENCHANTMENT_COUNT' by '1', and deletes a book, every time this function runs, until that count is '0'.

9. Lastly, back to `separate_enchantments_reward' the enchanted book, in the offhand of the player is deleted.

###########################################################################################################################################

*Phew!*

####################
Thank you again to all who made this 2011 dream a reality. With this complete system, the tedious grind is no more. Now, there's an exciting incentive to go fishing, use an enchanting table, and explore the world - excited for the contents of every treasure chest! (Jungle Pyramids, Strongholds, Dungeons, Mineshafts, Ancient Cities, Desert Pyramids, Pillager Outposts, Ocean Ruins, Trial Chambers, Bastion Remnants, and Woodland Mansions!) And no more need to infinitely expand the world file size seeking out the 20th Trial Chamber or Ancient City so everyone can get 'Wind Charge III' and 'Swift Sneak III'!

#######################################################################################################################